<template>
  <a-spin :spinning="confirmLoading">
    <a-form ref="formRef" class="antd-modal-form" :labelCol="labelCol" :wrapperCol="wrapperCol">
      <a-row>
        <a-divider type="vertical" class="antd-modal-divider" />
        <h4 class="hClass">基本信息</h4>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="姓名" v-bind="validateInfos.realname">
            <a-input v-model:value="formData.realname" placeholder="" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="8">
          <a-form-item label="头像" v-bind="validateInfos.avatar">
            <a-input v-model:value="formData.avatar" placeholder="请输入头像" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="8">
          <a-form-item label="性别" v-bind="validateInfos.sex">
            <JDictSelectTag v-model:value="formData.sex" dictCode="sex" :show-choose-option="false" placeholder=""
              :disabled="disabled" :stringToNumber="true" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="出生日期" v-bind="validateInfos.birthday">
            <a-date-picker placeholder="" v-model:value="formData.birthday" value-format="YYYY-MM-DD" style="width: 100%"
              :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="民族" v-bind="validateInfos.nation">
            <j-dict-select-tag v-model:value="formData.nation" dictCode="nation" :show-choose-option="false"
              placeholder="" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="籍贯" v-bind="validateInfos.nativePlace">
            <!-- <j-dict-select-tag v-model:value="formData.nativePlace" dictCode="sex" placeholder="" :disabled="disabled" /> -->
            <JAreaLinkage v-model:value="formData.nativePlace" :showArea="false" :showAll="false" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="政治面貌" v-bind="validateInfos.politicsFace">
            <j-dict-select-tag v-model:value="formData.politicsFace" dictCode="native_place" :show-choose-option="false"
              placeholder="" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="证件类型" v-bind="validateInfos.cardType">
            <j-dict-select-tag v-model:value="formData.cardType" dictCode="card_type" :show-choose-option="false"
              placeholder="" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="证件号码" v-bind="validateInfos.cardNumber">
            <a-input v-model:value="formData.cardNumber" placeholder="" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="8">
          <a-form-item label="电子邮件" v-bind="validateInfos.email">
            <a-input v-model:value="formData.email" placeholder="请输入电子邮件" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
        <a-col :span="8">
          <a-form-item label="联系方式" v-bind="validateInfos.phone">
            <a-input v-model:value="formData.phone" placeholder="" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
        <!-- <a-col :span="8">
          <a-form-item label="身份" v-bind="validateInfos.userIdentity">
            <j-dict-select-tag type='radio' v-model:value="formData.userIdentity" dictCode="sex" placeholder="请选择身份（1普通成员 2上级）" :disabled="disabled"/>
          </a-form-item>
        </a-col> -->
        <a-col :span="8">
          <a-form-item label="文化程度" v-bind="validateInfos.educational">
            <j-dict-select-tag v-model:value="formData.educational" dictCode="education" :show-choose-option="false"
              placeholder="" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="所学专业" v-bind="validateInfos.specialty">
            <j-dict-select-tag v-model:value="formData.specialty" dictCode="specialty" :show-choose-option="false"
              placeholder="" :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-divider type="vertical" class="antd-modal-divider" />
        <h4 class="hClass">部门信息</h4>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="所在部门" v-bind="validateInfos.orgCode">
            <JSelectDept v-model:value="formData.orgCode" placeholder="" :disabled="disabled" :showButton="false" style="width: 340px; " />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="当前岗位" v-bind="validateInfos.post">
            <JSelectPosition v-model:value="formData.post" placeholder=""  :showButton="false"  :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="8">
          <a-form-item label="员工编号" v-bind="validateInfos.workNo">
            <a-input v-model:value="formData.workNo" placeholder="" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="8">
          <a-form-item label="职称" v-bind="validateInfos.technicalTitle">
            <j-dict-select-tag v-model:value="formData.technicalTitle" dictCode="technical_title"
              :show-choose-option="false" placeholder="" :disabled="disabled" />
          </a-form-item>
        </a-col>
      </a-row>
      <a-row>
        <a-col :span="12">
          <a-form-item label="从事本技术领域起始时间" :labelCol="{ span: 10 }"  v-bind="validateInfos.startWorkDate">
            <a-date-picker placeholder="" v-model:value="formData.startWorkDate" value-format="YYYY-MM-DD"
              @change="changeDate" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <a-col :span="12">
          <a-form-item label="从事本技术领域年限" :labelCol="{ span: 10 }"  v-bind="validateInfos.workYear">
            <a-input-number v-model:value="formData.workYear" placeholder="" style="width: 100%" :disabled="disabled" />
          </a-form-item>
        </a-col>
        <!-- <a-col :span="8">
          <a-form-item label="本人简历(学历和工作经历）" v-bind="validateInfos.vitae">
            <a-input v-model:value="formData.vitae" placeholder="" :disabled="disabled"></a-input>
          </a-form-item>
        </a-col> -->
      </a-row>
    </a-form>
  </a-spin>
</template>

<script lang="ts" setup>
import { ref, reactive, defineExpose, nextTick, defineProps, computed } from 'vue';
import { useMessage } from '/@/hooks/web/useMessage';
import JDictSelectTag from '/@/components/Form/src/jeecg/components/JDictSelectTag.vue';
import { getValueType } from '/@/utils';
import { saveOrUpdate } from '../SysUser.api';
import { Form } from 'ant-design-vue';
import JSelectPosition from '/@/components/Form/src/jeecg/components/JSelectPosition.vue';
import JSelectDept from '/@/components/Form/src/jeecg/components/JSelectDept.vue';
import JAreaLinkage from '/@/components/Form/src/jeecg/components/JAreaLinkage.vue';


const props = defineProps({
  formDisabled: { type: Boolean, default: false },
  formData: { type: Object, default: () => { } },
  formBpm: { type: Boolean, default: true }
});
const formRef = ref();
const useForm = Form.useForm;
const emit = defineEmits(['register', 'ok']);
const formData = reactive<Record<string, any>>({
  id: '',
  realname: '',
  avatar: '',
  birthday: '',
  sex: undefined,
  email: '',
  phone: '',
  post: '',
  userIdentity: undefined,
  educational: '',
  technicalTitle: '',
  cardType: '',
  cardNumber: '',
  specialty: '',
  startWorkDate: '',
  workYear: undefined,
  vitae: '',
});
const { createMessage } = useMessage();
const labelCol = ref<any>({ xs: { span: 24 }, sm: { span: 7 } });
const wrapperCol = ref<any>({ xs: { span: 24 }, sm: { span: 12 } });
const confirmLoading = ref<boolean>(false);
//表单验证
const validatorRules = {
};
const { resetFields, validate, validateInfos } = useForm(formData, validatorRules, { immediate: true });

// 表单禁用
const disabled = computed(() => {
  if (props.formBpm === true) {
    if (props.formData.disabled === false) {
      return false;
    } else {
      return true;
    }
  }
  return props.formDisabled;
});



/**
 * 新增
 */
function add() {
  edit({});
}

/**
 * 编辑
 */
function edit(record) {
  nextTick(() => {
    resetFields();
    //赋值
    Object.assign(formData, record);
  });
}
// 计算从事本技术领域年限
function changeDate() {
  let startWorkDate = formData.startWorkDate
  let today = formatDate(new Date().toLocaleDateString());
  //从事本技术领域开始年月
  let oYear = startWorkDate.slice(0, 4);
  let oMon = startWorkDate.slice(5, 7);
  //当前年月
  let nYear = today.slice(0, 4);
  let nMon = today.slice(6, 7);
  //计算相差年月
  let year = parseInt(nYear) - parseInt(oYear);
  let mon = parseInt(nMon) - parseInt(oMon);
  var Ynum = parseInt(((year * 12 + mon) / 12));
  var Mnum = ((year * 12 + mon) % 12);

  if (Number(Mnum) <= 6) {
    formData.workYear = Ynum + 0.5;
  } else {
    formData.workYear = Ynum + 1;
  }
}

// 日期格式转换成YYYY-MM-DD
function formatDate(date) {
  var d = new Date(date),
    month = '' + (d.getMonth() + 1),
    day = '' + d.getDate(),
    year = d.getFullYear();

  if (month.length < 2) month = '0' + month;
  if (day.length < 2) day = '0' + day;

  return [year, month, day].join('-');
}
/**
 * 提交数据
 */
async function submitForm() {
  // 触发表单验证
  await validate();
  confirmLoading.value = true;
  const isUpdate = ref<boolean>(false);
  //时间格式化
  let model = formData;
  if (model.id) {
    isUpdate.value = true;
  }
  //循环数据
  for (let data in model) {
    //如果该数据是数组并且是字符串类型
    if (model[data] instanceof Array) {
      let valueType = getValueType(formRef.value.getProps, data);
      //如果是字符串类型的需要变成以逗号分割的字符串
      if (valueType === 'string') {
        model[data] = model[data].join(',');
      }
    }
  }
  await saveOrUpdate(model, isUpdate.value)
    .then((res) => {
      if (res.success) {
        createMessage.success(res.message);
        emit('ok');
      } else {
        createMessage.warning(res.message);
      }
    })
    .finally(() => {
      confirmLoading.value = false;
    });
}


defineExpose({
  add,
  edit,
  submitForm,
});
</script>

<style lang="less" scoped>
.antd-modal-form {
  min-height: 500px !important;
  overflow-y: auto;
  padding: 24px 24px 24px 24px;
}

.antd-modal-divider {
  width: 2px;
  height: 16px;
  background: #009688;
  border-radius: 0px 0px 0px 0px;
  opacity: 1;
}

.hClass {
  width: 60px;
  height: 20px;
  font-size: 14px;
  font-family: Microsoft YaHei-Regular, Microsoft YaHei;
  font-weight: 550;
  color: #009688;
  line-height: 14px;
}

.ant-input {
  width: 200px;
  height: 32px;
  border-radius: 4px 4px 4px 4px;
  opacity: 1;
  border: 1px solid #DDDDDD;
}

.ant-picker {
  width: 200px !important;
  height: 32px;
  border-radius: 4px 4px 4px 4px;
  opacity: 1;
  border: 1px solid #DDDDDD;
}

:deep(.ant-form-item-control-input) {
  width: 200px !important;
}
</style>
